package com.ynsj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynsj.domain.AdminBank;
import com.ynsj.domain.Config;
import com.ynsj.domain.Notice;
import com.ynsj.model.R;
import com.ynsj.service.ConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api(tags = "参数配置管理")
@RequestMapping("/configs")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @GetMapping
    @ApiOperation("系统参数配置分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "页大小"),
            @ApiImplicitParam(name = "type",value = "规则类型"),
            @ApiImplicitParam(name = "code",value = "规则代码"),
            @ApiImplicitParam(name = "name",value = "规则名称")
    })
    @PreAuthorize("hasAuthority('config_query')")
    public R<Page<Config>> findConfigPage(@ApiIgnore Page<Config> page, String type, String code, String name){
        return R.ok(configService.findByPage(page,type,code,name));
    }

    @PostMapping
    @ApiOperation("新增系统参数配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "config",value = "系统参数配置的json")
    })
    @PreAuthorize("hasAuthority('config_create')")
    public R addConfig(@RequestBody @Validated Config config){
        boolean isOk = configService.save(config);
        if (isOk){
            return R.ok("新增系统参数配置成功");
        }
        return R.fail("新增系统参数配置失败");
    }

    @PatchMapping
    @ApiOperation("修改系统参数配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "config",value = "系统参数配置的json")
    })
    @PreAuthorize("hasAuthority('config_update')")
    public R updateBank(@RequestBody @Validated Config config){
        boolean isOk = configService.updateById(config);
        if (isOk){
            return R.ok("更新系统参数配置成功");
        }
        return R.fail("更新系统参数配置失败");
    }
}
